package com.jianganming.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * 破坏单例方式二：反射
 */
public class Reflex2 {
    public static void main(String[] args)throws Exception {

        Class<Singleton> singletonClass = Singleton.class;
        Constructor<Singleton> constructor = singletonClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton singleton1 = constructor.newInstance();
        System.out.println(singleton1);
        Field singletonFlag = singletonClass.getDeclaredField("singletonFlag");
        singletonFlag.setAccessible(true);

        singletonFlag.setBoolean(singleton1,false);
        Singleton singleton = constructor.newInstance();
        System.out.println(singleton);
    }
}
